package com.bsoft.myapplication;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import java.util.Calendar;
import java.util.List;

/**
 * Created by 泅渡者
 * Created on 2017/4/1.
 */

public class DingService extends AccessibilityService {

    private String TAG = getClass().getSimpleName();
    private  boolean  isFinish = false;
    public static DingService instance;
    private int index = 1;

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        if(isFinish){
            return;
        }
        final AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if(nodeInfo == null) {
            Log.w(TAG, "rootWindow为空");
            return ;
        }

        System.out.println("index:"+index);
        switch (index) {
            case 1: //点击选中工作页面->点击签到
                inputClick("com.alibaba.android.rimet:id/home_bottom_tab_button_work");
                click("签到");
                index = 2;
                break;
            case 2: //点击签到进入详细信息页面
                System.out.println("View ID:"+nodeInfo.getViewIdResourceName());
                        Calendar ncalendar = Calendar.getInstance();
                            String Timer = ncalendar.get(Calendar.HOUR_OF_DAY)+":"+ ncalendar.get(Calendar.MINUTE);

                            click(Timer);



                break;
            case 3:
                doQianDao(event.getEventType(),nodeInfo);
                break;
            case 4:
                inputClick("com.alibaba.android.rimet:id/home_bottom_tab_icon");
                break;
            default:
                break;
        }
    }
    /**
     * 通过ID获取控件，并进行模拟点击
     * @param clickId
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    private void inputClick(String clickId) {
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if (nodeInfo != null) {
            List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId(clickId);
            for (AccessibilityNodeInfo item : list) {
                item.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        }
    }

    private void OpenQianDao(int type,AccessibilityNodeInfo nodeInfo) {
        if(type == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED){
            //判断当前是否是主页的签到页
            List<AccessibilityNodeInfo> qianList = nodeInfo.findAccessibilityNodeInfosByText("工作");
            if(!qianList.isEmpty()){
                boolean ret = click( "签到");
                index = 3;
                System.out.println("点击进入签到页面详情");
            }
        }
    }
    private void doQianDao(int type,AccessibilityNodeInfo nodeInfo) {
        if (type == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
            //判断当前页是否是签到页
            List<AccessibilityNodeInfo> case1 = nodeInfo.findAccessibilityNodeInfosByText("开启我的签到之旅");
            if (!case1.isEmpty()) {
                click("开启我的签到之旅");
                System.out.println("点击签到之旅");
            }

            List<AccessibilityNodeInfo> case2 = nodeInfo.findAccessibilityNodeInfosByText("我知道了");
            if (!case2.isEmpty()) {
                click("我知道了");
                System.out.println("点击我知道对话框");
            }
            List<AccessibilityNodeInfo> case3 = nodeInfo.findAccessibilityNodeInfosByText("签到");
            if (!case3.isEmpty()) {
                Toast.makeText(getApplicationContext(), "发现目标啦！！~~", Toast.LENGTH_SHORT).show();
                System.out.println("发现目标啦！");
                click("签到");
                isFinish = true;
            }
        }
    }
    //通过文字点击
    private boolean click(String viewText){
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if(nodeInfo == null) {
            Log.w(TAG, "点击失败，rootWindow为空");
            return false;
        }
        List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByText(viewText);
        if(list.isEmpty()){
            //没有该文字的控件
            Log.w(TAG, "点击失败，"+viewText+"没有该文字的控件");
            return false;
        }else{
            //有该控件
            //找到可点击的父控件
            AccessibilityNodeInfo view = list.get(0);
            return onclick(view);  //遍历点击
        }

    }
    private boolean onclick(AccessibilityNodeInfo view){
        if(view.isClickable()){
            view.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            Log.w(TAG, "点击成功");
            return true;
        }else{

            AccessibilityNodeInfo parent = view.getParent();
            if(parent==null){
                return false;
            }
            onclick(parent);
        }
        return false;
    }
    @Override
    public void onInterrupt() {

    }
    @Override
    protected void onServiceConnected() {
        // TODO Auto-generated method stub
        super.onServiceConnected();
        Log.i(TAG, "service connected!");
        Toast.makeText(getApplicationContext(), "连接成功！", Toast.LENGTH_SHORT).show();
        instance = this;
    }

    //点击返回按钮事件
    private void back(){
        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
    }

    public void setServiceEnable(){
        isFinish = false;
        Toast.makeText(getApplicationContext(), "服务可用开启！", Toast.LENGTH_SHORT).show();
        index = 1;
    }

}
